<? include("/inc/header.html") {} ?>
<style>
.row{
	margin-bottom: 10px;
}
</style>
<link rel="stylesheet" href="static/uploadify/uploadify.css" type="text/css"></link>
<script type="text/javascript" src="static/uploadify/jquery.uploadify.min.js"></script>
<script type="text/javascript" src="static/utf8-jsp/ueditor.config.js"></script>
<script type="text/javascript" src="static/utf8-jsp/ueditor.all.js"></script>
<script type="text/javascript" src="static/utf8-jsp/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" src="static/utf8-jsp/third-party/video-js/video.js"></script>
<script type="text/javascript" src="static/My97DatePicker/WdatePicker.js"></script>
<div class="col-lg-12" style="overflow: auto;height: 540px;">
	<div class="panel panel">
		<div class="panel-body pan">
			<div class="form-body pal">
				<div class="row">
					<label for="inputUsername" class="col-md-3 control-label">广告名称(只允许汉字、英文字母、数字及下划线,50字以内 )<span
						class='require'>*</span> </label>
					<div class="col-md-9">
						<div class="input-icon">
							<input id="name" type="text" class="form-control" />
						</div>
					</div>
				</div>
				<div class="row">
					<label for="inputUsername" class="col-md-3 control-label">文件类型
						<span class='require'>*</span> </label>
					<div class="col-md-9">
						<div class="input-icon">
							<select class="form-control" id="type">
									<!-- <option value="">全部</option> -->
									<option value="图片">图片</option>
									<!-- <option value="视频">视频</option> -->
								</select>
						</div>
					</div>
				</div>
				<div class="row">
					<label for="inputUsername" class="col-md-3 control-label">广告文件
						<span class='require'>*</span> </label>
					<div class="col-md-9">
						<div class="input-icon">
							<div class="input-group input-group-sm mbs">
								<input id="filePath" type="text" placeholder="请点击选择" class="form-control" disabled="true">
								 <span class="input-group-btn">
								 	<button type="button" data-toggle="dropdown" class="btn btn-blue dropdown-toggle" onclick="upFiles()">选择文件</button>
								</span>
						</div>
						</div>
					</div>
				</div>
				<div class="row">
					<label for="inputUsername" class="col-md-3 control-label">开始发布日期
						<span class='require'>*</span> </label>
					<div class="col-md-9">
						<div class="input-icon">
							<input id="startTime" type="text" class="form-control" placeholder="yyyy-MM-dd HH:mm:ss" readonly="readonly"/>
						</div>
					</div>
				</div>
				<div class="row">
					<label for="inputUsername" class="col-md-3 control-label">结束发布日期
						<span class='require'>*</span> </label>
					<div class="col-md-9">
						<div class="input-icon">
							<input id="endTime" type="text" class="form-control" placeholder="yyyy-MM-dd HH:mm:ss" readonly="readonly"/>
						</div>
					</div>
				</div>
				<div class="row">
					<label for="inputUsername" class="col-md-3 control-label">展示时长
						<span class='require'>*</span> </label>
					<div class="col-md-9">
						<div class="input-icon">
							<input id="showTime" type="text" class="form-control" />
						</div>
					</div>
				</div>
				<div class="row">
					<label for="inputUsername" class="col-md-3 control-label">关联课程
						<span class='require'>*</span> </label>
					<div class="col-md-9">
						<div class="input-icon">
							<div class="input-group input-group-sm mbs">
								<input id="courseName" type="text" placeholder="请点击选择" class="form-control" disabled="true">
								 <span class="input-group-btn">
								 	<button type="button" data-toggle="dropdown" class="btn btn-blue dropdown-toggle" onclick="advertisingKecheng()">选择课程</button>
								</span>
								<input id="courseId" type="hidden" class="form-control" >
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<label for="inputUsername" class="col-md-3 control-label">排序
						<span class='require'>*</span> </label>
					<div class="col-md-9">
						<div class="input-icon">
							<input id="sort" type="text" class="form-control" />
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<input id="id" type="hidden" value="${id}"/>
	<textarea id="uploadImage" name="uploadImage" style="display: none;"></textarea>
</div>
<script>
var _editor;
	$(function() {
		init();
		layui.use('laydate', function(){
		  var laydate = layui.laydate;
		  //执行一个laydate实例
		  laydate.render({
		     elem: '#startTime' //指定元素
			  ,type: 'datetime'
		  });
		});
		layui.use('laydate', function(){
		  var laydate = layui.laydate;
		  //执行一个laydate实例
		  laydate.render({
		     elem: '#endTime' //指定元素
			  ,type: 'datetime'
		  });
		});
	
	
		_editor = UE.getEditor('uploadImage');
	    _editor.ready(function (){
	        //设置编辑器不可用(事实上不可以设置不可用...所以注释掉,以观后效)
	        _editor.setDisabled();
	        //隐藏编辑器,因为只使用上传功能
	        _editor.hide();
	        //侦听图片上传
	  	 	_editor.addListener('beforeInsertImage',function(t,arg){
	  	 			for(var i=0;i<arg.length;i++){
	  	 				var urlPath = arg[i].src;
	  	 				$("#coverImg").val(urlPath);
						$("#single").html("<a href="+urlPath+" download='w3logo'><img src='"+urlPath+"' width='30' height='30' /></a>");
	  	 			}
	        });
	        //侦听文件上传
		   	 _editor.addListener('afterUpfile',function(t,arg){
		   	 	for(var i=0;i<arg.length;i++){
	  	 				var urlPath = arg[i].url;
	  	 				$("#filePath").val(urlPath);
	  	 			}
		   	 });
	   });
		
	});

//上传图片
function upImages() {
	var myFiles = _editor.getDialog("insertimage");
  		myFiles.open();
}
 //上传文件
function upFiles() {
	var myFiles = _editor.getDialog("attachment");
   	myFiles.open();
}
function init(){
	if(id!=null&&id!=''&&id!=undefined&&id!="undefined"&&id!="null"){
		$.ajax({
			type : "post",
			url : "./advertisementFind.do",
			data : {
				"id" : $("#id").val()
			},
			dataType : "json",
			success : function(data) {
				$("#name").val(data[0].name);
				$("#courseId").val(data[0].courseId);
				$("#courseName").val(data[0].courseName);
				$("#startTime").val(data[0].startTime);
				$("#endTime").val(data[0].endTime);
				$("#type").val(data[0].type);
				$("#filePath").val(data[0].filePath);
				$("#showTime").val(data[0].showTime);
				$("#sort").val(data[0].sort);
			},
			error : function(data) {
				alert("请求失败");
			}
		});
	}
}
function save(){
	var id='';
	if($("#id").val() != null 
		&& $("#id").val() != ''
		&& $("#id").val() != 'null'
		&& $("#id").val() != 'undefined'
		&&$("#id").val() != undefined){
		id=$("#id").val();
	}
	
	if($("#name").val()==''){
		layer.msg("请输入广告名称");
		return null;
	}
	
	var name=$("#name").val(); //    
    var re =/^[\u4e00-\u9fa5_a-zA-Z0-9]{1,50}$/;
	if (!re.test(name)) {
		layer.msg('广告名称:只允许汉字、英文字母、数字及下划线。50字以内');
		return null;
	}
	
	
	if($("#courseId").val()==''){
		layer.msg("请选择关联课程");
		return null;
	}
	if($("#startTime").val()==''){
		layer.msg("请选择开始发布日期");
		return null;
	}
	if($("#endTime").val()==''){
		layer.msg("请选择结束发布日期");
		return null;
	}
	if($("#type").val()==''){
		layer.msg("请选择类型");
		return null;
	}
	if($("#filePath").val()==''){
		layer.msg("请上传广告文件");
		return null;
	}
	if($("#showTime").val()==''){
		layer.msg("请输入展示时长");
		return null;
	}
	
	var price=$("#showTime").val(); //    
    var re =/^[0-9]+$/;
	if (!re.test(price)) {
		layer.msg('展示时长:单位为小时,整数！');
		return null;
	}
	
	
	if($("#sort").val()==''){
		layer.msg("请输入排序");
		return null;
	}
	
	var text=$("#sort").val(); //    
    var re =/^[1-9]*[1-9][0-9]*$/;
	if (!re.test(text)) {
		layer.msg('排序:输入在1-10000之间的整数！');
		return null;
	}
	var num = parseInt(text);
	if(num>10000 || num<1){
		layer.msg('排序:输入在1-10000之间的整数！');
		return null;
	}
	
	$.ajax({
		type : "post",
		url : "./advertisementAddUpdate.do",
		data : {
				id : id,	
				name:$("#name").val(),
				courseId:$("#courseId").val(),
				startTime:$("#startTime").val(),
				endTime:$("#endTime").val(),
				type:$("#type").val(),
				filePath:$("#filePath").val(),
				showTime:$("#showTime").val(),
				sort:$("#sort").val(),
		},
		dataType : "json",
		success : function(data) {
			if(data[0].result == 'success'){
				var index = parent.layer.getFrameIndex(window.name);
				window.parent.get();
				parent.layer.msg('提交成功');
				parent.layer.close(index);
			}else{
				parent.layer.msg(data[0].msg);
			}
		},
		error : function(data) {
			alert("请求失败");
		}
	});
}
function setCourse(id,name){
	$("#courseName").val(name);
	$("#courseId").val(id);
}
function advertisingKecheng(){
	parent.layer.open({
		type : 2,
		area : [ '70%', '80%' ],
		fix : false, //不固定
		scrollbar : true,
		shadeClose : true,
		//move : false,
		title:"课程列表",
		skin : 'layui-layer-rim', //加上边框
		btn : [ '保存 ', '关闭' ],
		content : ["advertising-kecheng.html?winName="+window.name, 'no'],
		yes : function(index,layer) {		
		 var iframeWin = parent.window[layer.find('iframe')[0]['name']];
		  			  iframeWin.save();
		},
		cancel : function(index) {
		}
	});
}
</script>